package kr.co.shoppingmall.board;

import java.awt.Component;
import java.util.HashMap;
import java.util.List;

import javax.swing.JOptionPane;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.istack.internal.logging.Logger;

@Controller
@RequestMapping("/board")
public class BoardController {
	/** log4j */
	public static void showMessageDialog(Component parentComponent, Object message){}
	protected final Log logger = LogFactory.getLog(getClass());
	
	@Resource
	private BoardService boardService;

	@RequestMapping("/Q&AWriteform.htm")
	public String writeform(BoardDTO dto, HttpSession session) {
		String id = (String)session.getAttribute("id");
		//System.out.println(id);
		if(id == null){
			JOptionPane.showMessageDialog(null, "로그인 후 사용해 주세요.");
			return "redirect:/index.htm";
		}
		else {
			return "/board/Q&AWriteform";
		}
	}

	@RequestMapping("/Q&AWrite.htm")
	public String write(BoardDTO dto, HttpSession session) {
		String id = (String)session.getAttribute("id");
		dto.setBoard_writer(id);
		boardService.insertBoard(dto);
		return "redirect:/board/Q&AList.htm";
	}

	@RequestMapping("/Q&AList.htm")
	public String list(
			@RequestParam(required = false, defaultValue = "1") int pg, Model model) {
		// pg 기본 값 : 1, 필수는 아님.
		int rowSize = 10;
		int start = (pg * rowSize) - (rowSize-1);
		int end = (pg * rowSize);
		HashMap map = new HashMap();
		map.put("start", start);
		map.put("end", end);
		List list = boardService.getBoardList(map);
		int rowCount = boardService.getCount();
		int linkSize = 10;
		int startLink = (pg-1) / linkSize * linkSize + 1;
		int endLink = (pg-1) / linkSize * linkSize + linkSize;
		int totalPage = rowCount / rowSize + (rowCount % rowSize==0 ? 0 : 1);
		if(endLink > totalPage) {
			endLink = totalPage;
		}
		int max = rowCount - ((pg-1) * rowSize);
		model.addAttribute("pg", pg);
		model.addAttribute("linkSize", linkSize);
		model.addAttribute("startLink", startLink);
		model.addAttribute("endLink", endLink);
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("max", max);
		model.addAttribute("list", list);
		return "/board/Q&AList";
	}//list

	@RequestMapping("/Q&ARead.htm")
	public String read(int num, int pg, Model model) {
		boardService.updateHit(num);
		BoardDTO dto = boardService.getBoard(num);
		model.addAttribute("b", dto);
		model.addAttribute("pg",pg);
		return "/board/Q&ARead";
	}

	@RequestMapping("/Q&AUpdateform.htm")
	public String updateform(int num, int pg, Model model, HttpSession session) {
		String id = (String)session.getAttribute("id");
		if(id == null){
			JOptionPane.showMessageDialog(null, "로그인 후 사용해 주세요.");
			return "redirect:/index.htm";
		}
		else{
			BoardDTO dto = boardService.getBoard(num);
			model.addAttribute("b", dto);
			model.addAttribute("pg", pg);
			return "/board/Q&AUpdateform";
		}
	}

	@RequestMapping("/Q&AUpdate.htm")
	public String update(BoardDTO dto, int pg) { 
		boardService.updateBoard(dto);
		return "redirect:/board/Q&AList.htm?pg="+pg;
	}

	@RequestMapping("/Q&ADeleteform.htm")
	public String deleteform(HttpSession session, Model model) {
		String id = (String)session.getAttribute("id");
		if(id == null){
			JOptionPane.showMessageDialog(null, "로그인 후 사용해 주세요.");
			return "redirect:/index.htm";
		}
		else{
			return "/board/Q&ADeleteform";
		}
	}

	@RequestMapping("/Q&ADelete.htm")
	public String delete(BoardDTO dto, int pg) {
		boardService.deleteBoard(dto);
		return "redirect:/board/Q&AList.htm?pg="+pg;
	}

	@RequestMapping("/Q&AReplyform.htm")
	public String replyform(int num, int pg, Model model, HttpSession session) {
		String id = (String)session.getAttribute("id");
		if(id == null){
			JOptionPane.showMessageDialog(null, "로그인 후 사용해 주세요.");
			return "redirect:/index.htm";
		}
		else{
			BoardDTO dto = boardService.getBoard(num);
			model.addAttribute("b", dto);
			model.addAttribute("pg", pg);
			return "/board/Q&AReplyform";
		}
	}

	@RequestMapping("/Q&AReply.htm")
	public String reply(BoardDTO dto, int pg, HttpSession session) {
		String id = (String)session.getAttribute("id");
		dto.setBoard_writer(id);
		dto.setBoard_sort(dto.getBoard_sort() + 1);
		dto.setBoard_tab(dto.getBoard_tab() + 1);
		boardService.updateReply(dto);
		boardService.insertReply(dto);
		return "redirect:/board/Q&AList.htm?pg="+pg;
	}
}//end